﻿using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace CPFUI.IControl
{
    [CPF.Design.DesignerLoadStyle("res://$safeprojectname$/Stylesheet1.css")]//用于设计的时候加载样式
    public class LedNumber : Control
    {
        public LedNumber()
        {
            if (_drawRect == Rect.Empty)
                _drawRect = new Rect(1, 1,this.Width.Value - 2, this.Height.Value - 2);
            _nums['0'] = new int[] { 1, 2, 3, 4, 5, 6 };
            _nums['1'] = new int[] { 2, 3 };
            _nums['2'] = new int[] { 1, 2, 5, 4, 7 };
            _nums['3'] = new int[] { 1, 2, 7, 3, 4 };
            _nums['4'] = new int[] { 2, 3, 6, 7 };
            _nums['5'] = new int[] { 1, 6, 7, 3, 4 };
            _nums['6'] = new int[] { 1, 6, 5, 4, 3, 7 };
            _nums['7'] = new int[] { 1, 2, 3 };
            _nums['8'] = new int[] { 1, 2, 3, 4, 5, 6, 7 };
            _nums['9'] = new int[] { 1, 2, 3, 4, 7, 6 };
            _nums['A'] = new int[] { 1, 2, 3, 5, 6, 7 };
            _nums['b'] = new int[] { 3, 4, 5, 6, 7 };
            _nums['C'] = new int[] { 1, 6, 5, 4 };
            _nums['c'] = new int[] { 7, 5, 4 };
            _nums['d'] = new int[] { 2, 3, 4, 5, 7 };
            _nums['E'] = new int[] { 1, 4, 5, 6, 7 };
            _nums['F'] = new int[] { 1, 5, 6, 7 };
            _nums['H'] = new int[] { 2, 3, 5, 6, 7 };
            _nums['h'] = new int[] { 3, 5, 6, 7 };
            _nums['J'] = new int[] { 2, 3, 4 };
            _nums['L'] = new int[] { 4, 5, 6 };
            _nums['o'] = new int[] { 3, 4, 5, 7 };
            _nums['P'] = new int[] { 1, 2, 5, 6, 7 };
            _nums['r'] = new int[] { 5, 7 };
            _nums['U'] = new int[] { 2, 3, 4, 5, 6 };
            _nums['-'] = new int[] { 7 };
            _nums[':'] = new int[0];
            _nums['.'] = new int[0];
            Value = '0';
        }
        /// <summary>
        /// 绘制区域
        /// </summary>
        Rect _drawRect = Rect.Empty;
        /// <summary>
        /// 数字集合
        /// </summary>
        private static Dictionary<char, int[]> _nums = new Dictionary<char, int[]>();

        /// <summary>
        /// 数值
        /// </summary>
        /// <value>The value.</value>
        [Description("数值"), Category("自定义")]
        public char Value
        {
            get { return GetValue<char>(); }
            set
            {
                if (!_nums.ContainsKey(value))
                {
                    return;
                }
                SetValue(value);

                Invalidate();
            }
        }
        /// <summary>
        /// 设置字符值
        /// </summary>
        /// <value>The character value.</value>
        public LedNumChar ValueChar
        {
            get
            {
                return (LedNumChar)((int)Value);
            }
            set
            {
                Value = (char)value;
            }
        }

        /// <summary>
        ///线条宽度
        /// </summary>
        private int _lineWidth = 8;

        /// <summary>
        /// 线条宽度
        /// </summary>
        /// <value>The width of the line.</value>
        [Description("线宽度，为了更好的显示效果，请使用偶数"), Category("自定义")]
        public int LineWidth
        {
            get { return _lineWidth; }
            set
            {
                _lineWidth = value;
                Invalidate();
            }
        }
        /// <summary>
        /// 获取或设置控件的前景色。
        /// </summary>
        /// <value>The color of the fore.</value>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
        /// </PermissionSet>
        [Description("颜色"), Category("自定义")]
        public Color ForeColor
        {
            get
            {
                return GetValue<Color>();
            }
            set
            {
                SetValue(value);
            }
        }
        //模板定义
        protected override void InitializeComponent()
        {
            DesiredSizeChanged += LedNumber_DesiredSizeChanged;
        }
        protected override void OnRender(DrawingContext dc)
        {
            base.OnRender(dc);
            using (Brush b = new SolidColorBrush(ForeColor))
            {
                if (Value == '.')
                {
                    Rect r2 = new Rect(_drawRect.Left + (_drawRect.Width - _lineWidth) / 2, _drawRect.Bottom - _lineWidth * 2, _lineWidth, _lineWidth);
                    dc.FillRectangle(b, r2);
                }
                else if (Value == ':')
                {
                    Rect r1 = new Rect(_drawRect.Left + (_drawRect.Width - _lineWidth) / 2, _drawRect.Top + (_drawRect.Height / 2 - _lineWidth) / 2, _lineWidth, _lineWidth);
                    dc.FillRectangle(b, r1);

                    Rect r2 = new Rect(_drawRect.Left + (_drawRect.Width - _lineWidth) / 2, _drawRect.Top + (_drawRect.Height / 2 - _lineWidth) / 2 + _drawRect.Height / 2, _lineWidth, _lineWidth);
                    dc.FillRectangle(b, r2);
                }
                else
                {
                    int[] vs = _nums[Value];
                    if (vs.Contains(1))
                    {
                        PathGeometry pathGeometry= new PathGeometry();
                        pathGeometry.BeginFigure(_drawRect.Left + 2, _drawRect.Top);
                        pathGeometry.LineTo(_drawRect.Right - 2, _drawRect.Top);
                        pathGeometry.LineTo(_drawRect.Right - _lineWidth - 2, _drawRect.Top + _lineWidth);
                        pathGeometry.LineTo(_drawRect.Left + _lineWidth + 2, _drawRect.Top + _lineWidth);
                        pathGeometry.LineTo(_drawRect.Left + 2, _drawRect.Top);
                        pathGeometry.EndFigure(true);
                        dc.FillPath(b, pathGeometry);
                    }

                    if (vs.Contains(2))
                    {
                        PathGeometry pathGeometry = new PathGeometry();
                        pathGeometry.BeginFigure(_drawRect.Right, _drawRect.Top);
                        pathGeometry.LineTo(_drawRect.Right, _drawRect.Top + (_drawRect.Height - _lineWidth - 4) / 2);
                        pathGeometry.LineTo(_drawRect.Right - _lineWidth / 2, _drawRect.Top + (_drawRect.Height - _lineWidth - 4) / 2 + _lineWidth / 2);
                        pathGeometry.LineTo(_drawRect.Right - _lineWidth, _drawRect.Top + (_drawRect.Height - _lineWidth - 4) / 2);
                        pathGeometry.LineTo(_drawRect.Right - _lineWidth, _drawRect.Top + _lineWidth);
                        pathGeometry.LineTo(_drawRect.Right, _drawRect.Top);
                        pathGeometry.EndFigure(true);
                        dc.FillPath(b, pathGeometry);
                    }

                    if (vs.Contains(3))
                    {
                        PathGeometry pathGeometry = new PathGeometry();

                        pathGeometry.BeginFigure(_drawRect.Right, _drawRect.Bottom - (_drawRect.Height - _lineWidth - 4) / 2);
                        pathGeometry.LineTo(_drawRect.Right, _drawRect.Bottom);
                        pathGeometry.LineTo(_drawRect.Right - _lineWidth, _drawRect.Bottom - _lineWidth);
                        pathGeometry.LineTo(_drawRect.Right - _lineWidth, _drawRect.Bottom - (_drawRect.Height - _lineWidth - 4) / 2);
                        pathGeometry.LineTo(_drawRect.Right - _lineWidth / 2, _drawRect.Bottom - (_drawRect.Height - _lineWidth - 4) / 2 - _lineWidth / 2);
                        pathGeometry.LineTo(_drawRect.Right, _drawRect.Bottom - (_drawRect.Height - _lineWidth - 4) / 2);

                        pathGeometry.EndFigure(true);
                        dc.FillPath(b, pathGeometry);
                    }

                    if (vs.Contains(4))
                    {
                        PathGeometry pathGeometry = new PathGeometry();
                        pathGeometry.BeginFigure(_drawRect.Left + 2, _drawRect.Bottom);
                        pathGeometry.LineTo(_drawRect.Right - 2, _drawRect.Bottom);
                        pathGeometry.LineTo(_drawRect.Right - _lineWidth - 2, _drawRect.Bottom - _lineWidth);
                        pathGeometry.LineTo(_drawRect.Left + _lineWidth + 2, _drawRect.Bottom - _lineWidth);
                        pathGeometry.LineTo(_drawRect.Left + 2, _drawRect.Bottom);
                        pathGeometry.EndFigure(true);
                        dc.FillPath(b, pathGeometry);


                       
                    }

                    if (vs.Contains(5))
                    {
                        PathGeometry pathGeometry = new PathGeometry();
                        pathGeometry.BeginFigure(_drawRect.Left, _drawRect.Bottom - (_drawRect.Height - _lineWidth - 4) / 2);
                        pathGeometry.LineTo(_drawRect.Left, _drawRect.Bottom);
                        pathGeometry.LineTo(_drawRect.Left + _lineWidth, _drawRect.Bottom - _lineWidth);
                        pathGeometry.LineTo(_drawRect.Left + _lineWidth, _drawRect.Bottom - (_drawRect.Height - _lineWidth - 4) / 2);
                        pathGeometry.LineTo(_drawRect.Left + _lineWidth / 2, _drawRect.Bottom - (_drawRect.Height - _lineWidth - 4) / 2 - _lineWidth / 2);
                        pathGeometry.LineTo(_drawRect.Left, _drawRect.Bottom - (_drawRect.Height - _lineWidth - 4) / 2);
                        pathGeometry.EndFigure(true);
                        dc.FillPath(b, pathGeometry);
                        
                    }


                    if (vs.Contains(6))
                    {
                        PathGeometry pathGeometry = new PathGeometry();
                        pathGeometry.BeginFigure(_drawRect.Left, _drawRect.Top);
                        pathGeometry.LineTo(_drawRect.Left, _drawRect.Top + (_drawRect.Height - _lineWidth - 4) / 2);
                        pathGeometry.LineTo(_drawRect.Left + _lineWidth / 2, _drawRect.Top + (_drawRect.Height - _lineWidth - 4) / 2 + _lineWidth / 2);
                        pathGeometry.LineTo(_drawRect.Left + _lineWidth, _drawRect.Top + (_drawRect.Height - _lineWidth - 4) / 2);
                        pathGeometry.LineTo(_drawRect.Left + _lineWidth, _drawRect.Top + _lineWidth);
                        pathGeometry.LineTo(_drawRect.Left, _drawRect.Top);
                        pathGeometry.EndFigure(true);
                        dc.FillPath(b, pathGeometry);

                    }

                    if (vs.Contains(7))
                    {
                        PathGeometry pathGeometry = new PathGeometry();
                        pathGeometry.BeginFigure(_drawRect.Left + _lineWidth / 2, _drawRect.Height / 2 + 1);
                        pathGeometry.LineTo(_drawRect.Left + _lineWidth, _drawRect.Height / 2 - _lineWidth / 2 + 1);
                        pathGeometry.LineTo(_drawRect.Right - _lineWidth, _drawRect.Height / 2 - _lineWidth / 2 + 1);
                        pathGeometry.LineTo(_drawRect.Right - _lineWidth / 2, _drawRect.Height / 2 + 1);
                        pathGeometry.LineTo(_drawRect.Right - _lineWidth, _drawRect.Height / 2 + _lineWidth / 2 + 1);
                        pathGeometry.LineTo(_drawRect.Left + _lineWidth, _drawRect.Height / 2 + _lineWidth / 2 + 1);
                        pathGeometry.LineTo(_drawRect.Left + _lineWidth / 2, _drawRect.Height / 2 + 1);
                        pathGeometry.EndFigure(true);
                        dc.FillPath(b, pathGeometry);
                       
                    }
                }
            }
        }
        private void LedNumber_DesiredSizeChanged(object sender, EventArgs e)
        {
            _drawRect = new Rect(1, 1, this.Width.Value - 2, this.Height.Value - 2);
        }

#if !DesignMode //用户代码写到这里，设计器下不执行，防止设计器出错
        protected override void OnInitialized()
        {
            base.OnInitialized();

        }
        //用户代码

#endif
    }
}
